
package com.eworking.eim.util;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class HttpClientRquest {
    
    /** 日志输出标记 */
    private static final String _TAG = HttpClientRquest.class.getSimpleName();
    
    /**
     * HTTPClient post请求
     * 
     * @param requestURL 请求地址
     * @param data 传输数据
     * @param headerMap head参数
     * @return
     */
    public static String post(String requestURL, String data, Map<String, String> headerMap) {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(requestURL);
        if (headerMap != null) {
            Iterator<Entry<String, String>> iterator = headerMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Entry<String, String> entry = iterator.next();
                post.addHeader(entry.getKey(), entry.getValue());
            }
        }
        String result = null;
        try {
            post.setEntity(new StringEntity(data));
            HttpResponse response = client.execute(post);
            int state = response.getStatusLine().getStatusCode();
            
            if (state == HttpStatus.SC_OK) {
                result = EntityUtils.toString(response.getEntity());
            } else {
                EIMLog.e(_TAG, "HttpClient请求失败，返回码：" + state);
                result = "返回码：" + state;
            }
        } catch (Exception localException) {
            EIMLog.e(_TAG, "HttpClient请求失败");
        }
        return result;
    }
    
    /**
     * HTTPClient get请求
     * 
     * @param requestURL 请求地址
     * @param paramMap 请求参数
     * @return
     */
    public static String get(String requestURL, Map<String, String> paramMap) {
        StringBuffer sb = new StringBuffer(512);
        sb.append(requestURL).append("?");
        HttpClient client = new DefaultHttpClient();
        if (paramMap != null) {
            Iterator<Entry<String, String>> iterator = paramMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Entry<String, String> entry = iterator.next();
                sb.append(entry.getKey()).append("=").append(entry.getValue());
                sb.append("&");
            }
            
            sb.substring(0, sb.length() - 1);
        }
        HttpGet httpget = new HttpGet(sb.toString());
        String result = "";
        try {
            HttpResponse response = client.execute(httpget);
            int state = response.getStatusLine().getStatusCode();
            if (state == 200) {
                result = EntityUtils.toString(response.getEntity());
            } else {
                EIMLog.e(_TAG, "HTTPClient请求失败，返回码：" + state);
                result = (new StringBuilder("返回码：")).append(state).toString();
            }
        } catch (Exception exception) {
            EIMLog.e(_TAG, "HTTPClient请求失败");
        }
        return result;
    }
}
